package com.shiver.base;

import org.apache.catalina.startup.Tomcat;

import java.io.File;

/**
 *
 * @author Programming is an art from.
 * @Description: TODO
 */
public class TomcatStart {

    public static int TOMCAT_PORT = 8080;
    public static String TOMCAT_HOSTNAME = "127.0.0.1";
    public static String WEBAPP_PATH = "src/main";
    public static String WEBINF_CLASSES = "/src/webapp/WEB-INF/classes";
    public static String CLASS_PATH = "target/classes";
    public static String INTERNAL_PATH = "/";

    private static int DEFAULT_PORT = 8080;

    public static void start() {
        /**
         * 实际项目中读取properties文件获得配置，
         * 这里就不拓展了，直接硬编码
         */

        Tomcat tomcat = new Tomcat();

        // 端口
        tomcat.setPort(DEFAULT_PORT);

        try {

            String docBase = "src/main/webapp/";

            String contextPath = "/";
            /**
             * tomcat加入web工程
             *
             * host:缺省默认为localhost
             * contextPath:在浏览器中访问项目的根路径
             * 例：localhost:port/{contextPath}/xx
             * docBase：项目中webapp所在路径
             *
             */
//            tomcat.addWebapp(host, contextPath, docBase)
            tomcat.addWebapp(contextPath, new File(docBase).getAbsolutePath());

            tomcat.start();
            System.out.println("tomcat服务启动成功。。");
            tomcat.getServer().await();
        } catch (Exception e) {
            System.out.println("tomcat服务启动失败。。");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        TomcatStart.start();
    }

}
